package com.runa.monitor.platinterface.meter3_0.handle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.runa.monitor.platinterface.meter3_0.command.CtrlValveResponse;
import com.runa.monitor.platinterface.meter3_0.command.SendBufferResponse;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.analysis.langkabo.LangKaBoFmuAnalysis;
import com.runa.rccprotocol.dto.AffirmDeny;
import com.runa.rccprotocol.dto.runa.RunaConcenAffirmDeny;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author diandian
 * @Date 2022/11/18 20:15
 */
@Component
@Slf4j
public class AckMessageHandler implements ResponseBusinessHandler {

    @Override
    public void handleBusiness(Map<String, Object> userProperties, TaskFunction function, String msg) {
        if (msg.contains("Rsp_Control_Valve")) {
            CtrlValveResponse resultVO = JSONObject.parseObject(msg, CtrlValveResponse.class);
            CtrlValveResponse.Data data = resultVO.getData();
            if (data == null) {
                function.doApply(CommStatus.OK, new RunaConcenAffirmDeny(false));
                return;
            }
            List<CtrlValveResponse.Valves> valves = data.getValves();
            if (CollectionUtils.isEmpty(valves)) {
                function.doApply(CommStatus.OK, new RunaConcenAffirmDeny(false));
                return;
            }
            for (CtrlValveResponse.Valves valveInfo : valves) {
                AffirmDeny affirmDeny = new RunaConcenAffirmDeny(valveInfo.getState());
                function.doApply(CommStatus.OK, affirmDeny);
            }
        } else if (msg.contains("Rsp_RecvBuffer")) {
            SendBufferResponse response = JSONObject.parseObject(msg, SendBufferResponse.class);
            SendBufferResponse.Data data = response.getData();
            if (data == null || !data.getResult()) {
                function.doApply(CommStatus.OK, new RunaConcenAffirmDeny(false));
                return;
            }
            //具体的报文解析
            log.info("琅卡博3.0档案下发回应------"+ JSON.toJSONString(data));
            String recvBuffer = data.getRecvBuffer();
            LangKaBoFmuAnalysis langKaBoFmuAnalysis = new LangKaBoFmuAnalysis();
            byte[] bytes = ByteUtil.hexStringToByte(recvBuffer);
            langKaBoFmuAnalysis.fill(bytes, bytes.length, "");
//            RunaConcenAffirmDeny affirmDeny = (RunaConcenAffirmDeny) langKaBoFmuAnalysis.gain();
//            function.doApply(CommStatus.OK, new RunaConcenAffirmDeny(affirmDeny.getData()));
            log.info("琅卡博3.0档案下发回应"+JSON.toJSONString(function));
            log.info("琅卡博3.0档案下发回应"+JSON.toJSONString(langKaBoFmuAnalysis.gain()));
            function.doApply(CommStatus.OK, langKaBoFmuAnalysis.gain());
        }
    }
}